package org.example.controller;

import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.example.service.complex.ComplexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/complex")
public class ComplexController {

    @Autowired
    ComplexService complexService;

    /** 部署流程定义 */
    @GetMapping("/deployWithClassPath")
    public String deployWithClassPath() {
        RepositoryService rs = ProcessEngines.getDefaultProcessEngine().getRepositoryService();
        Deployment deployment = rs.createDeployment()
                .name("复杂流程")
                .addClasspathResource("processes/complex1.bpmn")
                .deploy();
        rs.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
        return deployment.getId();
    }

    /** 开启一个流程实例 */
    @GetMapping("/startProcess")
    public String startProcess(@RequestParam(value = "orderNo") String orderNo) {
        return complexService.startProcess(orderNo);
    }

    /** 申请采购单 */
    @GetMapping("/applyPurchase")
    public String applyPurchase(@RequestParam(value = "taskId") String taskId,
                                @RequestParam(value = "orderNo") String orderNo,
                                @RequestParam(value = "amount") Double amount) {
        return complexService.applyPurchase(taskId, orderNo, amount);
    }

    /** 部门经理审核 */
    @GetMapping("/departmentReview")
    public String departmentReview(@RequestParam(value = "taskId") String taskId) {
        return complexService.departmentReview(taskId);
    }

    /** 项目经理审核 */
    @GetMapping("/projectReview")
    public String projectReview(@RequestParam(value = "taskId") String taskId) {
        return complexService.projectReview(taskId);
    }

    /** 财务审核 */
    @GetMapping("/financeReview")
    public String financeReview(@RequestParam(value = "taskId") String taskId) {
        return complexService.financeReview(taskId);
    }

    /** 总经理审核 */
    @GetMapping("/managerReview")
    public String managerReview(@RequestParam(value = "taskId") String taskId) {
        return complexService.managerReview(taskId);
    }

    /** 财务总监审核 */
    @GetMapping("/financeManagerReview")
    public String financeManagerReview(@RequestParam(value = "taskId") String taskId) {
        return complexService.financeManagerReview(taskId);
    }
}
